package com.ktjiaoyu.config;


import com.ktjiaoyu.cond.ConditionOnPropertyEnable;
import org.apache.commons.dbcp2.BasicDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
@PropertySource(value="classpath:database.properties",encoding="utf-8")
public class MyBatisConfig {
    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${pool.initialSize}")
    private int initialSize;
    @Value("${pool.maxTotal}")
    private int maxTotal;
    @Value("${pool.maxIdle}")
    private int maxIdle;
    @Value("${pool.minIdle}")
    private int minIdle;
    @Value("${pool.maxWaitMillis}")
    private int maxWaitMillis;
    @Value("${pool.removeAbandonedOnMaintenance}")
    private boolean removeAbandonedOnMaintenance;
    @Value("${pool.removeAbandonedOnBorrow}")
    private boolean removeAbandonedOnBorrow;
    @Value("${pool.removeAbandonedTimeout}")
    private int removeAbandonedTimeout;

    @Bean
    @Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
    @ConditionOnPropertyEnable(enableProperty = "dev.enable")
    public DataSource dataSource(){
        BasicDataSource dataSource=new BasicDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setInitialSize(initialSize);
        dataSource.setMaxTotal(maxTotal);
        dataSource.setMaxIdle(maxIdle);
        dataSource.setMinIdle(minIdle);
        dataSource.setMaxWaitMillis(maxWaitMillis);
        dataSource.setRemoveAbandonedOnMaintenance(removeAbandonedOnMaintenance);
        dataSource.setRemoveAbandonedOnBorrow(removeAbandonedOnBorrow);
        dataSource.setRemoveAbandonedTimeout(removeAbandonedTimeout);

        return dataSource;
    }

    @Bean
    @Conditional(ConditionOnPropertyEnable.OnEnableCondition.class)
    @ConditionOnPropertyEnable(enableProperty = "test.enable")
    public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
        SqlSessionFactoryBean factoryBean=new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        factoryBean.setTypeAliasesPackage("com.ktjiaoyu.entity");
        return factoryBean;
    }


}
